package com.jiamao.goplay.ui.discover;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.R;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.view.ScaleTransitionPagerTitleView;
import com.kongzue.dialog.interfaces.OnMenuItemClickListener;
import com.kongzue.dialog.v3.BottomMenu;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.BezierPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.SPUtils;
import skin.support.content.res.SkinCompatResources;

/*
 发现主页
 */
public class DiscoverHomeFragment extends BaseMVVMFragment {

    private static final String[] CHANNELS = new String[]{"附近", "发现"};
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private List<Fragment> fragmentList = new ArrayList();

    private ViewPager viewPager;
    CommonNavigator commonNavigator=null;

    FindFragment findFragment;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.discover_home_layout;
    }

    @Override
    public int initVariableId() {
        return 0;
    }

    @Override
    public void initData() {
        super.initData();
        ImmersionBar.setStatusBarView(this, binding.getRoot().findViewById(R.id.status_bar_view));
        initMagicIndicator6();
        initFragmetn();

        binding.getRoot().findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showBoomMenu();

            }
        });

        Messenger.getDefault().register(this, "appcolor", new BindingAction() {
            @Override
            public void call() {
                Log.d("HTML","BBBBBBBBBBBBBBBBBBBBBB");

               if(commonNavigator!=null) commonNavigator.notifyDataSetChanged();
            }


        });

    }


    private void initMagicIndicator6() {
        MagicIndicator magicIndicator = (MagicIndicator) binding.getRoot().findViewById(R.id.magic_indicator);
        viewPager = (ViewPager) binding.getRoot().findViewById(R.id.view_pager);
        commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {


                int defaule = SkinCompatResources.getInstance().getColor(getContext(),R.color.viewPager_title_defaul);
                int check = SkinCompatResources.getInstance().getColor(getContext(),R.color.viewPager_title_select);
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                simplePagerTitleView.setTextSize(18);
                simplePagerTitleView.setNormalColor(defaule);
                simplePagerTitleView.setSelectedColor(check);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
                indicator.setColors(Color.parseColor("#ff4a42"), Color.parseColor("#fcde64"), Color.parseColor("#73e8f4"), Color.parseColor("#76b0ff"), Color.parseColor("#c683fe"));
                return null;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position ==0 ){
                    binding.getRoot().findViewById(R.id.btn_add).setVisibility(View.INVISIBLE);
                }else{
                    binding.getRoot().findViewById(R.id.btn_add).setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


    private void initFragmetn(){
        findFragment = new FindFragment();
        fragmentList.add(new NearbyFragment());
        fragmentList.add(findFragment);
        MyFragmentPagerAdapter adapter= new MyFragmentPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);


        viewPager.setCurrentItem(1);

    }


    private void showBoomMenu(){
        BottomMenu.show((AppCompatActivity) getContext(), new String[]{"添加/搜索想去的地方", "筛选", "按照城市/国家查找"}, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
                    switch (index){
                        case 0:
                            startContainerActivity(SearchFragment.class.getCanonicalName());
                            break;
                        case 1:
                            showSexMenu();
                            break;
                        case 2:
                           // showCityMenu();
                            startContainerActivity(CityFragment.class.getCanonicalName());
                            break;
                    }
            }
        });
    }

    private void showSexMenu(){
        int sex = SPUtils.getInstance().getInt(Config.SEX);

        int pz = SPUtils.getInstance().getInt("pz",-1);
        String text;
        String[] a = new String[]{};
        if (pz==-1){
            if (sex == 0){
                text = "男";
                a = new String[]{"当前推荐：男","女","全部用户"};
            }else {
                text = "女";
                a = new String[]{"男","当前推荐：女","全部用户"};
            }

        }
        else if (pz == 1){
            text = "男";
            a = new String[]{"当前推荐：男","女","全部用户"};
        }else if(pz ==2){
            text = "女";
            a = new String[]{"男","当前推荐：女","全部用户"};
        }
        else  {
            text = "全部用户";
            a = new String[]{"男","女","当前推荐：全部用户"};
        }


        BottomMenu.show((AppCompatActivity) getContext(), a, new OnMenuItemClickListener() {
            @Override
            public void onClick(String text, int index) {
                //返回参数 text 即菜单名称，index 即菜单索引
             //   RxToast.showToastShort(text);
                switch (index){
                    case 0:
                        SPUtils.getInstance().put("pz",1);
                    findFragment.setSex(1);

                      //  RxToast.showToastShort("");
                       //startContainerActivity(SearchFragment.class.getCanonicalName());
                        break;
                    case 1:
                        SPUtils.getInstance().put("pz",2);
                        findFragment.setSex(2);
                        break;
                    case 2:
                        SPUtils.getInstance().put("pz",0);
                        findFragment.setSex(0);
                        //startContainerActivity(CityFragment.class.getCanonicalName());
                        break;
                }
            }
        });
    }
    public  void showCityMenu(){

    }



    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {


        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);

        }


        @Override
        public int getCount() {
            return CHANNELS.length;
        }

        @Override
        public Object instantiateItem(ViewGroup vg, int position) {
            return super.instantiateItem(vg, position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            super.destroyItem(container, position, object);
        }

        @Override
        public Fragment getItem(int position) {

            return fragmentList.get(position);
        }

    }



}
